<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            padding: 0;
            margin: 0;
        }

        .wrap {
            width: 300px;
            height: 500px;
            border: 2px solid #333;
            margin: 50px 500px;
            position: relative;
        }

        .bar {
            width: 296px;
            height: 50px;
            border: 2px solid orange;
            border-radius: 50px;
            position: absolute;
            bottom: 100px;
            left: 0;
        }
        
        .box {
            width: 50px;
            height: 50px;
            background-color: pink;
            border-radius: 50%;
            position: absolute;
        }
    </style>
</head>

<body>
    <div class="wrap">
        <div class="bar">
            <div class="box"></div>
        </div>
    </div>
</body>
<script>
    var box = document.getElementsByClassName("box")[0];
    var bar = document.getElementsByClassName("bar")[0];
    var wrap = document.getElementsByClassName("wrap")[0];

    var maxLeft = bar.clientWidth - box.clientWidth;
    var maxTop = bar.clientHeight - box.clientHeight;

    box.onmousedown = function (e) {
        var e = e || window.event;
        var posX = e.offsetX;
        var posY = e.offsetY;

        wrap.onmousemove = function (e) {
            var e = e || window.event;
            var x = e.pageX - bar.offsetLeft - posX - 500;
            var y = e.pageY - bar.offsetTop - posY - 50;
            console.log(e.pageX, e.pageY);
            console.log(x, y);

            if (x < 0) x = 0;
            if (y < 0) y = 0;
            if (x > maxLeft) x = maxLeft;
            if (y > maxTop) y = maxTop;

            box.style.left = x + "px";
            box.style.top = y + "px";

            if(x == maxLeft){
                location = "https://www.baidu.com";
            }
        }

        document.onmouseup = function () {
            box.style.left = "0";
            wrap.onmousemove = null;
        }
    }
</script>

</html>